package com.thinksns.sociax.t4.android.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.thinksns.sociax.api.Api;
import com.thinksns.sociax.t4.android.interfaces.InformationDetailsListener;
import com.thinksns.sociax.t4.android.video.ToastUtils;
import com.thinksns.sociax.t4.model.ModelBackMessage;
import com.thinksns.sociax.t4.model.ModelComment;
import com.thinksns.sociax.t4.model.ModelInformationCateList;
import com.thinksns.sociax.thinksnsbase.base.BaseListPresenter;
import com.thinksns.sociax.thinksnsbase.bean.ListData;
import com.thinksns.sociax.thinksnsbase.network.ApiHttpClient;

import java.io.Serializable;

/**
 * @author LeiYan
 * @describe
 * @date 2017/2/6
 * @contact email:907238552@qq.com
 */

public class InformationPresenter extends BaseListPresenter {
    private ResultHandler mResultHandler;
    private int mInformationId;
    private InformationDetailsListener mInformationDetailsListener;
    private Api.Information mInformationApi;
    private ModelComment mReplyComment;

    public InformationPresenter(Context context, int informationId, InformationDetailsListener listener) {
        super(context, null);
        mResultHandler = new ResultHandler();
        mInformationDetailsListener = listener;
        this.mInformationId = informationId;
    }

    /*未使用那些重载方法*/
    /*父类方法重载开始*/
    @Override
    public ListData parseList(String result) {
        return null;
    }

    @Override
    protected ListData readList(Serializable seri) {
        return null;
    }

    @Override
    public String getCachePrefix() {
        return null;
    }

    @Override
    public void loadNetData() {

    }

    /*父类方法重载开始结束*/
    public void loadDetails() {
        getApi().getInformationDetails(mInformationId, 0, new ApiHttpClient.HttpResponseListener() {
            @Override
            public void onSuccess(Object result) {
                if (mInformationDetailsListener != null && result != null) {
                    ModelInformationCateList mCateDetail = (ModelInformationCateList) result;
                    mInformationDetailsListener.loadDetailsSuccess(mCateDetail);

                }
            }

            @Override
            public void onError(Object result) {
                if (mInformationDetailsListener != null) {
                    mInformationDetailsListener.loadDetailsFailure(result);
                }
            }
        });
    }

    public void addComment(final ModelComment comment, final String realContent) {
        mReplyComment = comment;
        new Thread(new Runnable() {
            @Override
            public void run() {
                Object result = getApi().addComment(mInformationId, comment);
                Message msg = mResultHandler.obtainMessage();
                msg.obj = result;
                mReplyComment.setContent(realContent);
                msg.sendToTarget();
            }
        }).start();
    }

    public void deleteComment(final ModelComment modelComment){
        getApi().deleteComment(modelComment.getComment_id(), new ApiHttpClient.HttpResponseListener() {
            @Override
            public void onSuccess(Object result) {
                ModelBackMessage backMessage = (ModelBackMessage) result;
                ToastUtils.showToast(backMessage.getMsg());
                if (backMessage.isSuccess() && mInformationDetailsListener != null){
                    mInformationDetailsListener.deleteCommentSuccess(modelComment);
                }
            }

            @Override
            public void onError(Object result) {
                ToastUtils.showToast(String.valueOf(result));
            }
        });
    }

    public class ResultHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mInformationDetailsListener != null) {
                ModelBackMessage result = (ModelBackMessage) msg.obj;
                if (result == null){
                    result = new ModelBackMessage();
                }
                mInformationDetailsListener.sendCommentResult(result, mReplyComment);
            }
        }
    }

    private Api.Information getApi() {
        if (mInformationApi == null) {
            mInformationApi = new Api.Information();
        }
        return mInformationApi;
    }
}
